home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / localloginpw / pass.c < prev    next >
C/C++ Source or Header  |  1995-11-05  |  6KB  |  197 lines

  1. //---------------------------------------------------------------------------
  2. // PASSWORD                                        (c) Laurence Vanhelsuwe 1994
  3. // --------
  4. // This program is part of a group of 3 complimentary programs to log the
  5. // usage of a stand-alone machine. (LOGIN, LOGOUT, PASSWORD)
  6. //
  7. // This program has been written in ANSI C to make it fully portable across
  8. // different hardware platforms.
  9. //
  10. // Original development and testing was carried out on a Commodore Amiga 3000.
  11. //
  12. // History
  13. // -------
  14. // 03-JAN-94: Initial file creation.
  15. // 06-JAN-94: First useful version (with encoding)
  16. // 11-FEB-94: Fixed users listing bug ( "\r" ending prevented correct listing)
  17. //---------------------------------------------------------------------------
  18.  
  19. //#define        DEBUG            1
  20.  
  21. #include    "stdio.h"
  22. #include    "stdlib.h"
  23. #include    "string.h"
  24. #include    "fcntl.h"
  25. #include    "time.h"
  26.  
  27. #define        MAX_NAME_LEN    (10)
  28. #define        ENTRY_LEN        (1+ MAX_NAME_LEN*2)
  29.  
  30. #define        SET_FG_0        ""
  31.  
  32. #ifdef    AMIGA
  33. #define        PFILE            "DEVS:PASSWORDS.DAT"
  34. #define        HELP            "?"
  35. #define        SET_FG_1        ""
  36. #define        CREAT_MODE        (O_RDWR)
  37. #define        OPEN_MODE        (O_RDWR)
  38.  
  39. #else
  40.  
  41. #include    "sys\stat.h"
  42. #define        PFILE            "C:/DOS/PASSWOR.DAT"
  43. #define        HELP            "/?"
  44. #define        SET_FG_1        ""
  45. #define        CREAT_MODE        (S_IREAD | S_IWRITE)
  46. #define        OPEN_MODE        (O_RDWR | O_BINARY)
  47.  
  48. #endif
  49.  
  50. //---------------------------------------------------------------------------
  51. // Function Prototypes
  52. // -------------------
  53. //---------------------------------------------------------------------------
  54.  
  55. void get_password        (char *prompt_str, char *passw_str);
  56.  
  57. //---------------------------------------------------------------------------
  58. // Globals
  59. // -------
  60. //---------------------------------------------------------------------------
  61.  
  62. char sysop_password[40];
  63. char input[80];
  64. char username[MAX_NAME_LEN+2];
  65. char userpasw[MAX_NAME_LEN+2];
  66.  
  67. long int file_size, log_pos;
  68. int error, num_entries;
  69.  
  70. extern int errno;
  71.  
  72. int logfile, passwfile;            // LEVEL 1 ANSI C File Handles
  73.  
  74. time_t    systime;
  75. struct tm *tim;
  76.  
  77. //---------------------------------------------------------------------------
  78. //---------------------------------------------------------------------------
  79.  
  80. main(int argc, char **argv) {
  81.  
  82. int i,j,len, namelen, passwlen, num;
  83. char *s,*d;
  84.  
  85.     if(argc > 1 ) {
  86.         printf("PASSWORD V1.0  (Copyright (c) Jan 1994 L. Vanhelsuwe)\n\n");
  87.         printf("Programmed by Laurence Vanhelsuwe in C on an Amiga 3000.\n");
  88.         printf("Ported to IBM PC environment by L. Vanhelsuwe.\n\n");
  89.         printf("USAGE: PASSWORD [%s]\n\n", HELP);
  90.     }
  91.  
  92.     if ((passwfile = open(PFILE, OPEN_MODE)) == -1) {
  93. #ifdef DEBUG
  94.         printf("open(\"%s\",OPEN_MODE) returned %d...\n", PFILE, passwfile);
  95.         printf("ERROR # : %d \n", errno);
  96. #endif
  97.         printf("Passwords file does not exist ! Attempting to create it...\n");
  98.         passwfile = creat(PFILE, CREAT_MODE);
  99.         if (passwfile == -1) {
  100.             fprintf(stderr, "Couldn't create passwords file! Aborting...\n");
  101.             exit(10);
  102.         }
  103.         printf("Passwords file created OK.\n");
  104.     }
  105.  
  106.     get_password("Enter System Operator Password:", sysop_password);
  107.  
  108. // If the SYSOP enters the right PASSWORD immediately followed by 'USERS',
  109. // The programs dumps a list of all the valid users (decoded).
  110.  
  111.     if(strcmp(sysop_password, "AGRCRGAUSERS") == 0) {
  112.         printf("\nAuthorized Users List:\n\n");
  113.         printf  ("NUMBER\tUSERNAME  PASSWORD\n");
  114.         printf  ("------\t--------  --------\n");
  115.  
  116.         i = read(passwfile, input, ENTRY_LEN);
  117.         num = 1;
  118.         while (i==ENTRY_LEN) {
  119.             for(j=0;j<MAX_NAME_LEN;j++)
  120.                 if (input[j] != ' ') input[j]=input[j]-1;    // decode name
  121.             for(j=MAX_NAME_LEN;j<2*MAX_NAME_LEN;j++)
  122.                 if (input[j] != ' ') input[j]=input[j]-2;    // decode password
  123.  
  124.             printf("%3d\t", num++);
  125.             printf(input);                                    // print both
  126.             printf("\n");
  127.             i = read(passwfile, input, ENTRY_LEN);
  128.         }
  129.         printf("\n");
  130.         close(passwfile);
  131.         return 0;
  132.     }
  133.  
  134. // If the password wasn't the special listing password, then you have to enter
  135. // the normal one or you're out !
  136.  
  137.     if(strcmp(sysop_password, "AGRCRGA") != 0) {
  138.         printf("\nYou are not authorized to add users to this system.\n");
  139.         exit(20);
  140.     }
  141.  
  142.     lseek(passwfile, 0L, SEEK_END);            // goto the end to APPEND
  143.  
  144.     do {
  145.         printf("Enter new User's NAME     :");
  146.         scanf("%s", input);
  147.         namelen = strlen(input);
  148.         if (namelen > MAX_NAME_LEN)
  149.             printf("Please enter upto %d characters maximum..\n", MAX_NAME_LEN);
  150.     } while (namelen > MAX_NAME_LEN);
  151.     strcpy(username, input);
  152.  
  153.     do {
  154.         get_password("Enter new User's PASSWORD :", input);
  155.         passwlen = strlen(input);
  156.         if (passwlen > MAX_NAME_LEN)
  157.             printf("Please enter upto %d characters maximum..\n", MAX_NAME_LEN);
  158.         if (passwlen < 4)
  159.             printf("Please enter at least 4 characters..\n");
  160.  
  161.     } while (passwlen > MAX_NAME_LEN || passwlen < 4);
  162.     strcpy(userpasw, input);
  163.     
  164.     for(i=0, s=d=username; i<namelen ; i++) *d++ = (*s++) +1;
  165.     write(passwfile, username, namelen);
  166.     len = MAX_NAME_LEN - namelen;
  167.     if (len) write(passwfile, "          ", len);
  168.  
  169.     for(i=0, s=d=userpasw; i<passwlen; i++) *d++ = (*s++) +2;
  170.     write(passwfile, userpasw, passwlen);
  171.     len = MAX_NAME_LEN - passwlen;
  172.     if (len) write(passwfile, "          ", len);
  173.  
  174.     write(passwfile, "\r", 1);
  175.  
  176.     for(i=0, s=d=username; i<namelen ; i++) *d++ = (*s++) -1;
  177.     printf("\nUser '%s' added to list of Authorized Users\n", username);
  178.  
  179.     close(passwfile);
  180.  
  181.     return 0;
  182. }
  183.  
  184. //---------------------------------------------------------------------------
  185. // Get an invisible string in an ANSI compatible, portable way.
  186. //---------------------------------------------------------------------------
  187. void get_password        (char *prompt_str, char *passw_str) {
  188.  
  189.     printf(prompt_str);                // print the prompt
  190.  
  191.     printf(SET_FG_0);                // switch to WHITE foreground
  192.     scanf("%s", passw_str);            // get the string (cursor still moves...)
  193.     printf(SET_FG_1);                // switch back to normal foreground color
  194. }
  195.  
  196. //---------------------------------------------------------------------------
  197.